package com.longrise.android.byjk.plugins.tabfirst.allfunction;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/3/12.
 * 全部课程每个module的rcv的adapter
 */

public class AllFunctionRcvItem1Adapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    List<EntityBean> mBeanlist = new ArrayList<>();
    private OnAllFunctionItem1ClickListener mListener;

    public AllFunctionRcvItem1Adapter() {
        super(R.layout.item_home_allfunc_recycle, null);
    }


    public void setData(EntityBean[] beans) {
        if (beans == null) {
            return;
        }
        mBeanlist.clear();
        for (EntityBean bean : beans) {
            mBeanlist.add(bean);
        }
        setNewData(mBeanlist);
    }

    @Override
    protected void convert(BaseViewHolder helper, EntityBean item) {
        View convertView = helper.getConvertView();
        ImageView iv = (ImageView) convertView.findViewById(R.id.home_fragment_allfunc_rcv_iv);

        int width = AppUtil.getScreenWidth();
        int itemHeight = width / 4;
        ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
        layoutParams.height = itemHeight;
        convertView.setLayoutParams(layoutParams); //依照屏幕设置每个button的高度
        //动态配置每个imageview的宽高
        int ivwidth = AppUtil.getScreenWidth();
        int ivitemHeight = ivwidth / 4;
        ViewGroup.LayoutParams ivlayoutParams = iv.getLayoutParams();
        ivlayoutParams.height = (int) (ivitemHeight * 0.45);
        ivlayoutParams.width = (int) (ivitemHeight * 0.45);

        String picpath = item.getString("picpath");
        final String showname = item.getString("showname");
        final String uniqueid = item.getString("uniqueid");
        final String linkedtype = item.getString("linkedtype");
        final String linkedurl = item.getString("linkedurl");
        final String fullurl = item.getString("fullurl");

        Glide.with(AppUtil.getContext()).load(picpath).into(iv);
        helper.setText(R.id.home_fragment_allfunc_rcv_tv, showname);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(uniqueid,linkedtype,showname,linkedurl,fullurl);
                }
            }
        });
    }

    /**
     * 设置每个button的监听
     */
    public void setOnAllFunctionItem1ClickListener(OnAllFunctionItem1ClickListener listener) {
        this.mListener = listener;
    }

    public interface OnAllFunctionItem1ClickListener {
        void onClick(String uniqueid, String linkedtype, String showname, String linkedurl,
                     String fullurl);
    }
}
